package util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import bll.Enums;
import dal.ConnectionProvider;
import dal.GenericDal;
import model.*;

public class AuthenticationUtil {

	public static int doLogin(HttpServletRequest request, String username,
			String password) throws Exception {

		Connection dbConn = ConnectionProvider.startSelectTransaction();

		// GenericDal<AccountModel, AccountModelExt> dal = new
		// GenericDal<AccountModel, AccountModelExt>(
		// new AccountModel());
		GenericDal<AccountModel, AccountModelExt> dal = new GenericDal<AccountModel, AccountModelExt>(
				new AccountModel(), dbConn);
		AccountModel query = new AccountModel();
		query.setUsername(username);
		AccountModel account = dal.select(query);

		if (account == null) {
			return -1;
		}

		// check status NEW, ACTIVE, LOCKED
		if (account.getStatus() != Enums.AccountStatus.Active.getId()) {
			return -1;
		}

		int res = 0;

		// TODO temp for demo
		// String hashedPassword = CryptographyUtil.hashMD5(password);
		String hashedPassword = password;

		if (hashedPassword.equals(account.getPassword())) {
			SessionInfo session = new SessionInfo();
			session.setAccount(account);

			EmployeeModel employee = null;

			// GenericDal<EmployeeModel, EmployeeModelExt> _dal = new
			// GenericDal<EmployeeModel, EmployeeModelExt>(
			// new EmployeeModel());
			GenericDal<EmployeeModel, EmployeeModelExt> _dal = new GenericDal<EmployeeModel, EmployeeModelExt>(
					new EmployeeModel(), dbConn);

			EmployeeModelExt _query = new EmployeeModelExt();
			_query.setUserName(username);
			List<EmployeeModel> employees = _dal.selectList(_query);
			if (employees.size() > 0) {
				employee = employees.get(0);
			}

			session.setEmployee(employee);

			request.getSession().setAttribute(SessionKey.SESSION, session);

			// TODO init session

			if (account.getType() != Integer.MIN_VALUE) {
				res = account.getType();
			} else {
				res = Enums.AccountType.Staff.getId();
			}
		}

		ConnectionProvider.releaseTransaction(dbConn);

		return res;
	}

	public static boolean doLogout(HttpServletRequest request) {
		boolean res = false;

		request.getSession().removeAttribute(SessionKey.SESSION);

		// TODO save session info (if need)

		return res;
	}

	public static boolean checkLogin(HttpSession session) {
		boolean res = false;

		if (session.getAttribute(SessionKey.SESSION) != null) {
			res = true;
		}

		return res;
	}

	public static String getUserName(HttpSession session) {
		String userName = "";

		if (session.getAttribute(SessionKey.SESSION) != null) {
			SessionInfo sessionInfo = (SessionInfo) session
					.getAttribute(SessionKey.SESSION);
			AccountModel account = sessionInfo.getAccount();
			userName = account.getUsername();

			EmployeeModel employee = sessionInfo.getEmployee();
			if (employee != null) {
				userName += " (" + employee.getNameToString() + ")";
			}
		}

		return userName;
	}

	public static int getAccoutType(HttpSession session) {
		int accountType = -1;

		if (session.getAttribute(SessionKey.SESSION) != null) {
			SessionInfo sessionInfo = (SessionInfo) session
					.getAttribute(SessionKey.SESSION);
			accountType = sessionInfo.getAccount().getType();
		}

		return accountType;
	}

	public static boolean isDirectorLogined(HttpSession session) {
		boolean res = false;
		int accountType = getAccoutType(session);
		if (accountType == Enums.AccountType.Director.getId()) {
			res = true;
		}
		return res;
	}

}